//
//  HQHViewController.m
//  LastOne
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "HQHViewController.h"
#import "HQHListCollectionViewCell.h"
#import "HQHCollectionViewHeader.h"
#import "HQHFreeWriteCollectionViewCell.h"
#import "HTTPMethod.h"
#import "ChangeString.h"
#import "HQHListModel.h"
#import <UIImageView+WebCache.h>
#import "HQHSecondScrollViewController.h"
#import "HQHSecondListViewController.h"
#import "HQHFreeWriteDetailViewController.h"
#import "GiFHUD.h"
#import "FitHeader.h"
#import "NightHeader.h"
@interface HQHViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, PushToNextViewControllerDelegate, PushToNextViewControll>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)UICollectionView *collectionView;
@property(nonatomic, strong)NSMutableArray *listDataSourceArray;
@property(nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic, strong)NSDictionary *listDic;
@property(nonatomic, strong)NSMutableArray *typeDataSource;

@end

@implementation HQHViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    self.navigationItem.title = @"阅读";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
}

-(void)loadView
{
    [super loadView];
    
    [self getListData];
    
    self.typeDataSource = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(117 * FitWidth, 117 * FitHeight);
    flowLayout.minimumInteritemSpacing = 6 * FitWidth;
    flowLayout.minimumLineSpacing = 6 * FitHeight;
    flowLayout.sectionInset = UIEdgeInsetsMake(6 * FitHeight, 4 * FitWidth, 6 * FitHeight, 4 * FitWidth);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 554 * FitHeight) collectionViewLayout:flowLayout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [_collectionView NightWithType:UIViewColorTypeNormal];
    
    [self.view addSubview:_collectionView];
    
    [_collectionView registerClass:[HQHListCollectionViewCell class] forCellWithReuseIdentifier:@"identifier"];
    
    [_collectionView registerClass:[HQHCollectionViewHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
    
    [_collectionView registerClass:[HQHFreeWriteCollectionViewCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    
    [GiFHUD setGifWithImageName:@"loading.gif"];
    
    
}

-(void)getListData
{
    [GiFHUD show];
    self.listDataSourceArray = [NSMutableArray array];
    self.dataSource = [NSMutableArray array];
    [HTTPMethod getDataByString:@"http://api2.pianke.me/read/columns" BodyDic:[ChangeString changeStringToDictionary:@"auth=&client=1&deviceid=137794B8-2578-4119-B189-3E532E7C0F5B&version=3.0.6"] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        _listDataSourceArray = [dataDic objectForKey:@"list"];
        for (_listDic in _listDataSourceArray) {
            HQHListModel *listModel = [[HQHListModel alloc]init];
            [listModel setValuesForKeysWithDictionary:_listDic];
            [_dataSource addObject:listModel];
            
            [_typeDataSource addObject:[_listDic objectForKey:@"type"]];
            
        }
        
        [_collectionView reloadData];
        [GiFHUD dismiss];

    }];
}




-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 9;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HQHListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    
    if (_dataSource.count != 0) {
       cell.listModel = _dataSource[indexPath.row];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    HQHSecondListViewController *secondListVC = [[HQHSecondListViewController alloc]init];
    secondListVC.type = _typeDataSource[indexPath.row];
    [self.navigationController pushViewController:secondListVC animated:YES];
}




#pragma mark Header&Footer

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    HQHCollectionViewHeader *headView;
    HQHFreeWriteCollectionViewCell *footerView;
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        headView.myDelegate = self;
        return headView;
    }
    else if ([kind isEqual:UICollectionElementKindSectionFooter])
    {
        footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        footerView.myPushDelegate = self;
        return footerView;
    }
    
    return nil;
}


-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(375 * FitWidth, 180 * FitHeight);
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(375 * FitWidth, 180 * FitHeight);
}

-(void)ScrollViewPush:(NSString *)ID
{
    HQHSecondScrollViewController *scrollViewVC = [[HQHSecondScrollViewController alloc]init];
    scrollViewVC.ID = ID;
    [self.navigationController pushViewController:scrollViewVC animated:YES];
}

-(void)pushAction
{
    HQHFreeWriteDetailViewController *freeWriteVC = [[HQHFreeWriteDetailViewController alloc]init];
    [self.navigationController pushViewController:freeWriteVC animated:YES];
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
